package com.zpark.spring_02_ioc.test;

import com.zpark.spring_02_ioc.dao.IAccountDao;
import com.zpark.spring_02_ioc.pojo.Emp;
import com.zpark.spring_02_ioc.pojo.Empty;
import com.zpark.spring_02_ioc.pojo.User;
import com.zpark.spring_02_ioc.service.IAccountService;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @PackageName: com.zpark.spring_02_ioc.test
 * @ClassName: Client
 * @Description:
 * @author: RZS
 * @date: 2023/3/23  14:59
 */
public class Client {

    @Test
    public void test1(){
        // 1、创建spring容器对象
        ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
        // 2、根据ID获取bean对象
        IAccountDao accountDao = app.getBean("accountDao", IAccountDao.class);
        IAccountService accountService = (IAccountService) app.getBean("accountService");
        // 3、使用bean
        System.out.println(accountDao);
        System.out.println(accountService);
    }

    @Test
    public void test2(){
        // 1、使用beanFactory创建容器
        Resource resource = new ClassPathResource("beans.xml");
        BeanFactory beanFactory = new XmlBeanFactory(resource);

        // 2、通过id获取bean对象
        IAccountDao accountDao = (IAccountDao) beanFactory.getBean("accountDao");

        accountDao.saveAccount();
    }

    @Test
    public void test3(){
        // 1、创建spring容器对象
        ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
        // 2、根据ID获取bean对象
        User user1 = app.getBean("user", User.class);
        User user2 = app.getBean("user", User.class);
        User user3 = app.getBean("user", User.class);
        User user4 = app.getBean("user", User.class);

        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user3);
        System.out.println(user4);
    }


    @Test
    public void test4(){
        // 1、创建spring容器对象
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
        // 2、根据ID获取bean对象
        User user1 = app.getBean("user", User.class);
        System.out.println(user1);

        // 关闭容器
        app.close();
    }

    @Test
    public void test5(){
        // 1、创建spring容器对象
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
        // 2、根据ID获取bean对象
        Empty empty = app.getBean("empty", Empty.class);
        System.out.println(empty);

        // 关闭容器
        app.close();
    }

    @Test
    public void test6(){
        // 1、创建spring容器对象
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
        // 2、根据ID获取bean对象
        Emp emp = app.getBean("instanceStaticFactory", Emp.class);
        System.out.println(emp);

        // 关闭容器
        app.close();
    }


    @Test
    public void test7(){
        // 1、创建spring容器对象
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
        // 2、根据ID获取bean对象
        Emp emp = app.getBean("emp", Emp.class);
        System.out.println(emp);

        // 关闭容器
        app.close();
    }
}
